import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension; 
import java.awt.Rectangle; 
import java.io.IOException;
import java.util.Random;

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane;
import javax.swing.JTextArea; 
import javax.swing.JLabel; 
import javax.swing.JTextField;

public class MainFrame extends JFrame {
	
	static int horizMapSize = 0;
	static int vertMapSize = 0;
	static int horizEnd = 0;
	static int vertEnd = 0;
	static int currentHealth = 0;
	static int oneUp = 0;
	static int loadHealth = 0;
	static int loadMapIndex1 = 0;
	static int loadMapIndex2 = 0;
	static int loadTurn = 0;
	static int regenHealth = 0;
	static int gameOver = 0;
	static String eatFood;
	public JTextField lifeCount;
  public JTextField currentMapIndex1; 
  public JTextField currentMapIndex2;

  public MainFrame(String title) {
    	
    	setMinimumSize(new Dimension(640, 480));
      setMaximumSize(new Dimension(640, 480)); 
      setMaximizedBounds(new Rectangle(0, 0, 0, 0)); 
      setBounds(new Rectangle(0, 0, 640, 480));
      getContentPane().setSize(new Dimension(640, 480)); 
      getContentPane().setPreferredSize(new Dimension(640, 480)); 
      setPreferredSize(new Dimension(640, 480)); 
      setTitle("Simple Adventure"); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      getContentPane().setLayout(null);
 
        // super("Simple Adventure");                                                                              
        // Create Swing Component                               
 
        JButton btnEast = new JButton("East"); 
        btnEast.setBounds(371, 358, 100, 50); 
        getContentPane().add(btnEast);                               
 
        JButton btnWest = new JButton("West"); 
        btnWest.setBounds(173, 358, 100, 50); 
        getContentPane().add(btnWest);                
 
        JButton btnNorth = new JButton("North"); 
        btnNorth.setBounds(272, 332, 100, 50); 
        getContentPane().add(btnNorth);                 
 
        JButton btnSouth = new JButton("South"); 
        btnSouth.setBounds(272, 381, 100, 50); 
        getContentPane().add(btnSouth);  
                
        JButton btnLoad = new JButton("Load"); 
        btnLoad.setBounds(19, 321, 100, 50);
        getContentPane().add(btnLoad);
                
        JButton btnBegin = new JButton("New Game"); 
        btnBegin.setBounds(19, 381, 100, 50);
        getContentPane().add(btnBegin);
                
        JButton btnSave = new JButton("Save"); 
        btnSave.setBounds(512, 321, 100, 50);
        getContentPane().add(btnSave);
               
        JButton btnExit = new JButton("Exit"); 
        btnExit.setBounds(512, 381, 100, 50);
        getContentPane().add(btnExit);
 
        
        final JTextArea statusWindow = new JTextArea();
        statusWindow.setEditable(false);
        JScrollPane sp = new JScrollPane(statusWindow);
        sp.setBounds(10, 69, 604, 249);
        getContentPane().add(sp);
        

        JLabel lblLife = new JLabel("Life"); 
        lblLife.setBounds(40, 10, 80, 20);
        getContentPane().add(lblLife);
                
        lifeCount = new JTextField(); 
        lifeCount.setBounds(38, 36, 26, 26); 
        getContentPane().add(lifeCount); 
        lifeCount.setColumns(10);
 
        JLabel lblCurrent = new JLabel("Current");
        lblCurrent.setBounds(275, 10, 80, 20);
        getContentPane().add(lblCurrent);                
 
        JLabel lblLocation = new JLabel("Location"); 
        lblLocation.setBounds(325, 10, 80, 20);
        getContentPane().add(lblLocation);               
        
        currentMapIndex1 = new JTextField(); 
        currentMapIndex1.setBounds(297, 36, 26, 26);
        getContentPane().add(currentMapIndex1); 
        currentMapIndex1.setColumns(10);                
 
        currentMapIndex2 = new JTextField(); 
        currentMapIndex2.setBounds(323, 36, 26, 26); 
        getContentPane().add(currentMapIndex2); 
        currentMapIndex2.setColumns(10); 
 
        btnBegin.addActionListener(new ActionListener() { 
        	@Override 
        	public void actionPerformed(ActionEvent arg0) {
        	int horizDistance = 0;
        	int vertDistance = 0;
        	gameOver = 0;
        	currentHealth = Generator.genHealth(currentHealth);
        	oneUp = Generator.genOneUp(oneUp);

        	//This value ensures that there is a great enough distance from the start point to end point.
        	//Currently a Test Value. Change before Release.
        	while (horizDistance < 1){
        		horizMapSize = Generator.genHoriz(horizMapSize);
        		horizEnd = Generator.endHoriz(horizEnd);
        		if (horizMapSize - horizEnd < 2 || horizEnd - horizMapSize < 2){
        			horizMapSize = Generator.genHoriz(horizMapSize);
            		horizEnd = Generator.endHoriz(horizEnd);
        		}
            	if (horizMapSize - horizEnd > 1 || horizEnd - horizMapSize > 1) {
            		horizDistance ++;
            	}
        	}
        	
        	//This value ensures that there is a great enough distance from the start point to end point.
        	//Currently a Test Value. Change before Release.
        	while (vertDistance < 1){
            	vertMapSize = Generator.genVert(vertMapSize);
            	vertEnd = Generator.endVert(vertEnd);
            	if (vertMapSize - vertEnd < 2 || vertEnd - vertMapSize < 2){
        			vertMapSize = Generator.genHoriz(vertMapSize);
            		vertEnd = Generator.endHoriz(vertEnd);
        		}
            	if (vertMapSize - vertEnd > 2 || vertEnd - vertMapSize > 2){
            		vertDistance ++;
            	}
        	}
        	updateUI();
        	System.out.println("Health Display Value = " + currentHealth + ".");
            System.out.println("Horizontal Display Value = " + horizMapSize + ".");
            System.out.println("Vertical Display Value = " + vertMapSize + "."); 
            System.out.println("Horizontal End Value = " + horizEnd + ".");
            System.out.println("Vertical End Value = " + vertEnd + ".");
            System.out.println("The random value for Health increase must equal = " + oneUp + ".");                
            }  

            public void updateUI() {
            	main.status = "You have started a New Game.";
            	statusWindow.append(main.status + "\n"); 
                String healthUI = Integer.toString(currentHealth);
                lifeCount.setText(healthUI);
                String horizUI = Integer.toString(horizMapSize);
                currentMapIndex1.setText(horizUI);
                String vertUI = Integer.toString(vertMapSize);
                currentMapIndex2.setText(vertUI);
            }      
        });
        
        btnLoad.addActionListener(new ActionListener() { 
        	@Override 
        	public void actionPerformed(ActionEvent arg0) { 
        		try {
        			currentHealth = Storage.loadHealth(loadHealth);
        			horizMapSize = Storage.loadMapIndex1(loadMapIndex1);
        			vertMapSize = Storage.loadMapIndex2(loadMapIndex2);
        			updateUI(); 
        			System.out.println("Health Display Value = " + currentHealth + ".");
        			System.out.println("Horizontal Display Value = " + horizMapSize + ".");
        			System.out.println("Vertical Display Value = " + vertMapSize + ".");
        			
        		} catch (IOException e) {
        			// TODO Auto-generated catch block
        			updateFail();
        			e.printStackTrace();
        			}
            }  
        	
        	public void updateFail() { 
        		main.status = "No saved Game found to Load.";
        		statusWindow.append(main.status + "\n");
        		}

        	public void updateUI() { 
        		main.status = "You have loaded a Saved Game.";
        		statusWindow.append(main.status + "\n"); 
                String healthUI = Integer.toString(currentHealth);
                lifeCount.setText(healthUI);
                String horizUI = Integer.toString(horizMapSize);
                currentMapIndex1.setText(horizUI);
                String vertUI = Integer.toString(vertMapSize);
                currentMapIndex2.setText(vertUI);
            }       
        });

        btnExit.addActionListener(new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent arg0) { 
            	System.exit(0);         
            }
        });
                
        btnSave.addActionListener(new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent arg0) {
            	try {
            		Storage.saveGame();
					updateUI();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					}
            	}
            
            public void updateUI() {
            	main.status = "You have saved the Current Game.";
            	statusWindow.append(main.status + "\n"); 
            }
        });
    
        btnNorth.addActionListener(new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent arg0) {
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd || gameOver == 1){
            		main.status = "Please start a New Game.";
            		statusWindow.append(main.status + "\n");
            	}
            	else if (horizMapSize != horizEnd || vertMapSize != vertEnd){
            		regenHealth = Generator.genRegen(regenHealth);
                	eatFood = Generator.genFood(eatFood);
                	if (eatFood == "banana"){
                		addHealth.health2();
                	}
                	if (regenHealth == oneUp) {
                		addHealth.health();
                		main.status = "You find a " + eatFood + " to eat and gain Health!";
                		statusWindow.append(main.status + "\n");
                	}
                	if (currentHealth <= 0) {
                		main.status = "You have run out of Life. Please start a New Game.";
                		statusWindow.append(main.status + "\n");
                		gameOver = 1;
                	}            		
                	else {
                		currentHealth = currentHealth - 1;
                		horizMapSize = horizMapSize + 1;
                		main.status = "You travel to the North.";
                		updateUI();
                	}
                	
            	}
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd && gameOver == 0){
            		gameOver = 1;
            		main.status = "Congratulations, You have reached your destination.";
            		statusWindow.append(main.status + "\n");
            	}	
            }
            
            public void updateUI() {
        		statusWindow.append(main.status + "\n"); 
                String healthUI = Integer.toString(currentHealth);
                lifeCount.setText(healthUI);
                String horizUI = Integer.toString(horizMapSize);
                currentMapIndex1.setText(horizUI);
                String vertUI = Integer.toString(vertMapSize);
                currentMapIndex2.setText(vertUI);
            }
        });
        
        btnSouth.addActionListener(new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent arg0) {
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd || gameOver == 1){
            		main.status = "Please start a New Game.";
            		statusWindow.append(main.status + "\n");
            	}
            	else if (horizMapSize != horizEnd || vertMapSize != vertEnd){
            		regenHealth = Generator.genRegen(regenHealth);
                	eatFood = Generator.genFood(eatFood);
                	if (eatFood == "banana"){
                		addHealth.health2();
                	}
                	if (regenHealth == oneUp) {
                		addHealth.health();
                		main.status = "You find a " + eatFood + " to eat and gain Health!";
                		statusWindow.append(main.status + "\n");
                	}
                	if (currentHealth <= 0) {
                		main.status = "You have run out of Life. Please start a New Game.";
                		statusWindow.append(main.status + "\n");
                		gameOver = 1;
                	}            		
                	else {
                		currentHealth = currentHealth - 1;
                		horizMapSize = horizMapSize - 1;
                		main.status = "You travel to the South.";
                		updateUI();
                	}
                	
            	}
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd && gameOver == 0){
            		gameOver = 1;
            		main.status = "Congratulations, You have reached your destination.";
            		statusWindow.append(main.status + "\n");
            	}	
            }
            
            public void updateUI() {
        		statusWindow.append(main.status + "\n"); 
                String healthUI = Integer.toString(currentHealth);
                lifeCount.setText(healthUI);
                String horizUI = Integer.toString(horizMapSize);
                currentMapIndex1.setText(horizUI);
                String vertUI = Integer.toString(vertMapSize);
                currentMapIndex2.setText(vertUI);
            }
        });
        
        btnEast.addActionListener(new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent arg0) {
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd || gameOver == 1){
            		main.status = "Please start a New Game.";
            		statusWindow.append(main.status + "\n");
            	}
            	else if (horizMapSize != horizEnd || vertMapSize != vertEnd){
            		regenHealth = Generator.genRegen(regenHealth);
                	eatFood = Generator.genFood(eatFood);
                	if (eatFood == "banana"){
                		addHealth.health2();
                	}
                	if (regenHealth == oneUp) {
                		addHealth.health();
                		main.status = "You find a " + eatFood + " to eat and gain Health!";
                		statusWindow.append(main.status + "\n");
                	}
                	if (currentHealth <= 0) {
                		main.status = "You have run out of Life. Please start a New Game.";
                		statusWindow.append(main.status + "\n");
                		gameOver = 1;
                	}            		
                	else {
                		currentHealth = currentHealth - 1;
                		vertMapSize = vertMapSize + 1;
                		main.status = "You travel to the East.";
                		updateUI();
                	}
                	
            	}
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd && gameOver == 0){
            		gameOver = 1;
            		main.status = "Congratulations, You have reached your destination.";
            		statusWindow.append(main.status + "\n");
            	}	
            }
            
            public void updateUI() {
        		statusWindow.append(main.status + "\n"); 
                String healthUI = Integer.toString(currentHealth);
                lifeCount.setText(healthUI);
                String horizUI = Integer.toString(horizMapSize);
                currentMapIndex1.setText(horizUI);
                String vertUI = Integer.toString(vertMapSize);
                currentMapIndex2.setText(vertUI);
            }
        });
        
        btnWest.addActionListener(new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent arg0) {
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd || gameOver == 1){
            		main.status = "Please start a New Game.";
            		statusWindow.append(main.status + "\n");
            	}
            	else if (horizMapSize != horizEnd || vertMapSize != vertEnd){
            		regenHealth = Generator.genRegen(regenHealth);
                	eatFood = Generator.genFood(eatFood);
                	if (eatFood == "banana"){
                		addHealth.health2();
                	}
                	if (regenHealth == oneUp) {
                		addHealth.health();
                		main.status = "You find a " + eatFood + " to eat and gain Health!";
                		statusWindow.append(main.status + "\n");
                	}
                	if (currentHealth <= 0) {
                		main.status = "You have run out of Life. Please start a New Game.";
                		statusWindow.append(main.status + "\n");
                		gameOver = 1;
                	}            		
                	else {
                		currentHealth = currentHealth - 1;
                		vertMapSize = vertMapSize - 1;
                		main.status = "You travel to the West.";
                		updateUI();
                	}
                	
            	}
            	if (horizMapSize == horizEnd && vertMapSize == vertEnd && gameOver == 0){
            		gameOver = 1;
            		main.status = "Congratulations, You have reached your destination.";
            		statusWindow.append(main.status + "\n");
            	}            	
            }
            
            public void updateUI() {
        		statusWindow.append(main.status + "\n"); 
                String healthUI = Integer.toString(currentHealth);
                lifeCount.setText(healthUI);
                String horizUI = Integer.toString(horizMapSize);
                currentMapIndex1.setText(horizUI);
                String vertUI = Integer.toString(vertMapSize);
                currentMapIndex2.setText(vertUI);
            }
        }); 
        
    }
 
}